package bev;

import java.sql.Timestamp;

import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.JTextArea;

public class HelperUtility {
    // Formats a date into a standard format to be used across the application.
    public static String formatDate( Date date ) {
        if ( date == null )
            return "'null'";

        DateFormat df = new SimpleDateFormat( "MM/dd/yyyy" );
        return df.format( date );
    }

    // Formats a Timestamp into a standard format to be used across the
    // application.
    public static String formatTimeStamp( Timestamp timestamp ) {
        if ( timestamp == null )
            return "'null'";

        Date date = new Date( timestamp.getTime() );

        // Date format.
        DateFormat dfDate = new SimpleDateFormat( "MMMM d, y",
                Locale.getDefault() );
        dfDate.setTimeZone( Calendar.getInstance().getTimeZone() );

        // Time format.
        DateFormat dfTime = new SimpleDateFormat( "h:mm a", Locale.getDefault() );
        dfTime.setTimeZone( Calendar.getInstance().getTimeZone() );

        return String.format( "%s at %s", dfDate.format( date ),
                dfTime.format( date ).toLowerCase() );
    }

    // Returns a string representing a zero date.
    public static String getDatabaseZeroDate() {
        return "00/00/0000 00:00:00";
    }

    /**
     * Returns a Timestamp set to the 1st of January 1 A.D.
     */
    public static Timestamp getZeroTimestamp() {
        Calendar cal = Calendar.getInstance();
        cal.set( 0, Calendar.JANUARY, 1, 0, 0, 0 );
        return new Timestamp( cal.getTime().getTime() );
    }

    // Formats a Timestamp into a standard format to submit to database.
    public static String formatDatabaseTimeStamp( Timestamp timestamp ) {
        if ( timestamp == null )
            return "0000-00-00 00:00:00";

        Calendar cal = Calendar.getInstance();
        cal.setTime( timestamp );
        if ( cal.get( Calendar.YEAR ) < 1000 )
            return "0000-00-00 00:00:00";

        Date date = new Date( timestamp.getTime() );
        DateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        return df.format( date );
    }

    // Loops through all of a component's sub components and sets their
    // 'enabled' values.
    public static void setSubComponentsEnabled( JComponent component,
            boolean enabled ) {
        for ( Component cmp : component.getComponents() ) {
            cmp.setEnabled( enabled );
        }
    }

    // Attempts to convert a string to an integer value.
    public static int parseStringToInt( String value ) {
        try {
            return Integer.parseInt( value );
        } catch ( NumberFormatException e ) {
            System.err
                    .printf(
                            "ERROR: Failed to parse string as int.\nValue: %s\n",
                            value );
            return Integer.MIN_VALUE;
        }
    }

    public static void showNotImplementedMessage( JFrame parent ) {
        JOptionPane.showMessageDialog( parent,
                "This feature hasn't been implemented yet." );
    }

    // Returns the text of a JTextField and checks if the instance is null
    // before
    // dereferencing it. If the JTextField is null, an empty string is returned
    // (i.e. "").
    public static String getTextNullCheck( JTextField txtFld ) {
        return txtFld != null ? txtFld.getText() : "";
    }

    // Returns the text of a JTextArea and checks if the instance is null before
    // dereferencing it. If the JTextArea is null, an empty string is returned
    // (i.e. "").
    public static String getTextNullCheck( JTextArea txtArea ) {
        return txtArea != null ? txtArea.getText() : "";
    }

    // Returns the text of a JTextField converted as an int and checks if the
    // instance
    // is null before dereferencing it. If the JTextField is null, 0 is
    // returned.
    public static int getTextAsIntNullCheck( JTextField txtFld ) {
        return txtFld != null ? parseStringToInt( txtFld.getText() ) : 0;
    }

    // Capitalizes only the first character of a string.
    public static String capitalizeFirstOnly( String value ) {
        if ( value == null || value.length() == 0 )
            return "";

        char[] retValue = value.toLowerCase().toCharArray();
        retValue[0] = Character.toUpperCase( retValue[0] );
        return new String( retValue );
    }

    // Returns the values of a string or an empty
    // string if the String passed in is null.
    public static String stringOrDefault( String value ) {
        return value != null ? value : "";
    }
}
